package com.dlc.shop.user.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.app.dto.ProductDto;
import com.dlc.shop.bean.enums.ProdStatusEnums;
import com.dlc.shop.bean.enums.ProdType;
import com.dlc.shop.bean.model.Product;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.util.Arith;
import com.dlc.shop.service.ProductService;
import com.dlc.shop.user.common.dao.UserLevelCategoryMapper;
import com.dlc.shop.user.common.dao.UserLevelMapper;
import com.dlc.shop.user.common.model.UserLevel;
import com.dlc.shop.user.common.model.UserLevelCategory;
import com.dlc.shop.user.common.service.UserLevelCategoryService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Service
@AllArgsConstructor
public class UserLevelCategoryServiceImpl extends ServiceImpl<UserLevelCategoryMapper, UserLevelCategory> implements UserLevelCategoryService {

    private final UserLevelCategoryMapper userLevelCategoryMapper;
    private final UserLevelMapper userLevelMapper;
    private final ProductService productService;

    @Override
    public List<Long> getCategoryIdByLevelId(Long id) {
        List<Long> categoryIdByLevelId = userLevelCategoryMapper.getCategoryIdByLevelId(id);
        return categoryIdByLevelId;
    }

    @Override
    public void insertBatch(Long[] categorys ,Long id) {
        userLevelCategoryMapper.insertBatch(categorys, id);
    }

    @Override
    public void delBatch(Long[] categorys, Long id) {
        userLevelCategoryMapper.delBatch(categorys, id);
    }

    @Override
    public List<ProductDto> listDiscountProdByLevel(Integer size, Long levelId, Long shopId) {
        UserLevel userLevel = userLevelMapper.selectById(levelId);
        if (userLevel == null) {
            return new ArrayList<>();
        }
        if (userLevel.getDiscount() >= Constant.MAX_LEVEL_DISCOUNT) {
            return new ArrayList<>();
        }
        // 查出折扣商品
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getShopId, shopId)
                .ne(Product::getProdType, ProdType.PROD_TYPE_ACTIVE.value())
                .eq(Product::getStatus, ProdStatusEnums.NORMAL.getValue());
        if (userLevel.getDiscountType() == 1) {
            // 指定商品打折，则查出分类目录
            List<UserLevelCategory> levelCategories = this.list(new LambdaQueryWrapper<UserLevelCategory>()
                    .eq(UserLevelCategory::getLevelId, levelId));
            if (CollectionUtils.isEmpty(levelCategories)) {
                return null;
            }
            List<Long> categoryIds = levelCategories.stream().map(UserLevelCategory::getCategoryId).collect(Collectors.toList());
            wrapper.in(Product::getCategoryId, categoryIds);
        }
        List<Product> products;
        // 限制返回数据的大小
        if (size == null) {
            products = productService.list(wrapper);
        } else {
            products = productService.page(new Page<>(1, 20), wrapper).getRecords();
        }
        if (CollectionUtils.isEmpty(products)) {
            return new ArrayList<>();
        }
        // 转成productDTO，并计算商品店铺会员价格
        List<ProductDto> productDtos = new ArrayList<>(products.size());
        ProductDto productDto;
        for (Product product : products) {
            productDto = new ProductDto();
            BeanUtils.copyProperties(product, productDto);
            double memberPrice =  Arith.div(Arith.mul(product.getPrice(), userLevel.getDiscount()), 10, 2);
            //避免出现0.005的情况 先乘以100再除以100
            memberPrice = Arith.div(Arith.mul(memberPrice, 100), 100, 2);
            productDto.setMemberPrice(Math.max(memberPrice, 0.01));
            productDtos.add(productDto);
        }
        return productDtos;
    }
}
